import pymysql


class MyMySQL(object):
    def __init__(self):
        self.conn = pymysql.connect(host='39.106.25.58',port=3306,user='root',passwd='Zjp921421.',db='Student_data',charset='utf8mb4')
        self.cursor = self.conn.cursor()
        self.status = False

    def __del__(self):
        self.conn.close()

    def insert(self, **user):
        """插入用户数据"""
        sql = f"INSERT INTO Student(name, age,addr, phone, academic) VALUES('{user.get('username')}','{user.get('age')}','{user.get('addr')}','{user.get('phone')}','{user.get('academic')}')"
        try:
            self.cursor.execute(sql)
            self.conn.commit()
            self.status = True
            return self.status
        except Exception as e:
            print("添加用户失败，原因市%s"%e)
            return self.status

    def select(self):
        sql = f"SELECT * FROM Student "
        try:
            self.cursor.execute(sql)
            user = self.cursor.fetchall()
            return user
        except Exception as e:
            print("查询失败,原因是%s"%e)